use crate::init::*;
use crate::types::*;

pub unsafe fn mi_heap_get_default() -> *mut mi_heap_t {
    mi_thread_init();
    mi_get_default_heap()
}

pub static mut default_heap: libc::pthread_key_t = 0;

pub unsafe fn mi_get_default_heap() -> *mut mi_heap_t {
    let mi_heap_default = libc::pthread_getspecific(default_heap) as *mut mi_heap_t;
    if mi_heap_default.is_null() {
        return &mut _mi_heap_empty;
    }
    mi_heap_default
}

pub unsafe fn mi_set_default_heap(heap:*mut mi_heap_t) {
    libc::pthread_setspecific(default_heap, heap as _);
}

pub unsafe fn _mi_heap_random(heap: *mut mi_heap_t) -> usize {
    let r = (&mut *heap).random;
    (&mut *heap).random = _mi_random_shuffle(r);
    r
}

macro_rules! mi_heap_stat_increase {
    ($heap:expr, $stat:ident, $amount:expr) => {
        crate::stats::mi_stat_increase(&mut (&mut *(&mut *$heap).tld).stats.$stat, $amount)
    };
}

macro_rules! mi_heap_stat_decrease {
    ($heap:expr, $stat:ident, $amount:expr) => {
        crate::stats::mi_stat_decrease(&mut (&mut *(&mut *$heap).tld).stats.$stat, $amount)
    };
}

pub(crate) use mi_heap_stat_decrease;
pub(crate) use mi_heap_stat_increase;
//pub unsafe fn mi_heap_stat_increase(heap: *mut mi_heap_t, mount: usize) {
//    mi_stat_increase((&mut *(&mut * heap).tld).segments.stats, mount);
//}
//
//pub unsafe fn mi_heap_stat_decrease(heap: *mut mi_heap_t, mount: usize) {
//    mi_stat_update(stat, -(mount as i64));
//}
